﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

[assembly: CLSCompliant(true)]
namespace ContractManagementSystem.DataAccess
{
    public class SqlProvider : IDisposable
    {
        #region Property
        public string ErrorMessage { get; set; }
        #endregion
        #region Feild
        private readonly string _connectionString;
        private SqlConnection _sqlConnection;
        #endregion
        
        /// <summary>
        /// Constructor
        /// Author: Group2.TuanCH - 2h52PM 07/04/2012
        /// </summary>
        public SqlProvider()
        {
            ErrorMessage = string.Empty;
            try
            {
                if (_connectionString == null)
                {
                    _connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                }

                if (_sqlConnection == null)
                {
                    _sqlConnection = new SqlConnection(_connectionString);
                }
            }
            catch (NullReferenceException nullReferenceException)
            {
                ErrorMessage = nullReferenceException.Message;
            }
            catch (ConfigurationErrorsException configurationErrorsException)
            {
                ErrorMessage = configurationErrorsException.Message;
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;
            }
            catch
            {
                ErrorMessage = "Some Error";
                throw;
            }
        }

        /// <summary>
        /// CreateConnection
        /// Author: Group2.TuanCH - 2h54PM 07/04/2012
        /// </summary>
        /// <returns> SqlConnection with status Open </returns>
        public SqlConnection CreateConnection()
        {
            try
            {
                if (_connectionString != null && _sqlConnection == null)
                {
                    _sqlConnection = new SqlConnection(_connectionString);
                }

                if (_sqlConnection.State == ConnectionState.Closed)
                {
                    _sqlConnection.Open();
                }
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;
            }

            return _sqlConnection;
        }

        /// <summary>
        /// Close connection
        /// Author: Group2.TuanCH - 2h55PM 07/04/2012
        /// </summary>
        public void CloseConnection()
        {
            try
            {
                if (_sqlConnection.State == ConnectionState.Open)
                {
                    _sqlConnection.Close();
                }
            }
            catch (SqlException sqlException)
            {
                ErrorMessage = sqlException.Message;
            }
        }
       
        /// <summary>
        /// Dispose
        /// Author: Group2.TuanCH - 2h58PM 07/04/2012
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // dispose managed resources
                if (_sqlConnection != null)
                {
                    if (_sqlConnection.State == ConnectionState.Open)
                    {
                        _sqlConnection.Close();
                    }

                    _sqlConnection.Dispose();
                }
            }
            // free native resources
        }

        /// <summary>
        /// Dispose
        /// Author: Group2.TuanCH - 2h59PM 07/04/2012
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
